<?php

namespace Home\Controller;

use Think\Controller;
use Think\View;
use Think\Model;

class IndexController extends Controller {
	public function __construct() {
		session_start ();
		parent::__construct ();
	}
	// 展示首页
	public function index() {
		$this->display ( 'front/index' ); // 无参默认调用index.html
	}
	// 登录页
	public function admin() {
		$this->display ( 'admin/login' ); // 无参默认调用index.html
	}
	// 登录首页
	public function login() {
		if (! isset ( $_POST ['submit'] )) { // 检验是否为登陆状态
			$this->newobj1 = new \Home\Model\newsModel ();
			$this->newobj2 = new \Home\Model\usersModel();
			$this->newobj3 = new \Home\Model\columnModel();
			$this->newobj4 = new \Home\Model\adminModel();
			$this->newobj5 = new \Home\Model\roleModel();
			$date=array(
					'newsnum'=>$this->newobj1->count (),
					'usersnum'=> $this->newobj2->count (),
					'columnnum'=>$this->newobj3->count (),
					'adminnum'=>$this->newobj4->count (),
					'rolenum'=>$this->newobj5->count ()
			);
			$this->assign ( 'date', $date );
			$this->display ( 'admin/admin_index' );
		} else {
			$this->checklogin ();
		}
	}
	// 检查登录
	private function checklogin() {
		if (empty ( $_POST ['username'] ) || empty ( $_POST ['password'] )) {
			$this->showmessage ( '登录失败！', 'index.php?m=home&c=index&a=index' );
		}
		$username = ($_POST ['username']);
		$password = ($_POST ['password']);
		$this->authobj = new \Home\Model\authModel ();
		if ($auth = $this->authobj->checkauth ( $username, $password )) {
			$_SESSION ['auth'] = $auth;
			$this->showmessage ( '登录成功！', 'index.php?m=home&c=index&a=login' );
		} else {
			$this->showmessage ( '登录失败！', 'index.php?m=home&c=index&a=index' );
		}
	}
	// 修改密码
	public function changepwd() {
		if ($_POST ['password1'] != $_POST ['password2']) {
			echo "<script>alert('两次密码输入不同，请重新输入');</script>";
		}
		if ($_POST ['password1'] == $_POST ['password2'] && $_POST ['password1'] != '' && $_POST ['password2'] != '') {
			$where = $_SESSION ['auth'];
			$data = M ( 'admin' )->where ( $where )->find ();
			$update ['password'] = $_POST ['password1'];
			M ( 'admin' )->where ( $where )->save ( $update );
			
			$this->showmessage ( '您的密码密码修改成功，请重新登录', 'index.php?m=Home&c=Index&a=index' );
		} else {
			$this->display ( "admin/changepwd" );
		}
	}
	// 退出登录
	public function logout() {
		unset ( $_SESSION ['auth'] );
		$this->showmessage ( '退出成功！', 'index.php?m=home&c=index&a=index' );
	}
	//生成首页
	public function createindex(){
		
		if(file_put_contents('Application/Home/View/index.html', 'vadvdsvsdvsdvddads')){
			$this->showmessage ( '首页生成成功', 'index.php?m=home&c=index&a=login' );
		};
		
	}
	//用户列表
	public function userlist(){
		$data = $this->getuserlist ();
		// var_dump($data);
		$this->assign ( array (
				'data' => $data
		) );
		$this->display ( 'admin/userlist' );
	}
	// 增加新闻
	public function newsadd() {
		// $this->display('index/newsadd');
		if (! isset ( $_POST ['submit'] )) {
			$data = $this->getnewsinfo ();
			$data1 = $this->getcolumnlist ();
			$this->assign ( array (
					'data1' => $data1
			) );
			$this->assign ( array (
					'data' => $data 
			) );
			$this->display ( 'admin/newsadd' );
		} else {
			$this->newssubmit ();
		}
	}
	// 添加新闻2
	private function newssubmit() {
		extract ( $_POST );
	
		if (empty ( $title ) || empty ( $content )) {
			$this->showmessage ( '请把新闻标题、内容填写完整再提交！', 'index.php?m=home&c=index&a=newsadd' );
		}
		$newsobj = M ( 'news' );
		$data = array (
				'title' => $title,
				'content' => $content,
				'author' => $author,
				'from' => $from,
				'column' => $column,
				'dateline' => time ()
		);
		if ($_POST ['id'] != 0) {
			$where = array (
					'id' => $_POST ['id']
			);
			M ( 'news' )->where ( $where )->save ( $data );
			$this->showmessage ( '修改成功！', 'index.php?m=home&c=index&a=newslist' );
		} else {
			M ( 'news' )->add ( $data );
			$this->showmessage ( '添加成功！', 'index.php?m=home&c=index&a=newslist' );
		}
	}
	//增加管理员
	public function adminadd() {
		// $this->display('index/newsadd');
		if (! isset ( $_POST ['submit'] )) {
			$data = $this->getrolelist ();
			// var_dump($data);
			$this->assign ( array (
					'data' => $data
			) );
			$this->display ( 'admin/adminadd' );
		} else {
			$data = array (
					'username' => $_POST ['username'],
					'password' => $_POST ['password'],
					'role' => $_POST ['role']
			);
			if (M ( 'admin' )->add ( $data )) {
				$this->showmessage ( '管理员添加成功', 'index.php?m=home&c=index&a=adminlist' );
			} else
				$this->showmessage ( '管理员添加失败', 'index.php?m=home&c=index&a=adminadd' );	
		}
	}
	// 增加角色
	public function roleadd() {
		$this->display ( 'admin/roleadd' );
	}
	// 存储角色
	public function addrole() {
		$data = array (
				'role' => $_POST ['role'],
				'ManageRole' => $_POST ['ManageRole'],
				'ManageNews' => $_POST ['ManageNews'],
				'ManageUser' => $_POST ['ManageUser'],
				'ManageAdmin' => $_POST ['ManageAdmin'],
				'ManagePI' => $_POST ['ManagePI'] 
		);
		if (M ( 'role' )->add ( $data )) {
			$this->showmessage ( '角色添加成功', 'index.php?m=home&c=index&a=rolelist' );
		} else
			$this->showmessage ( '角色添加失败', 'index.php?m=home&c=index&a=roleadd' );
	}
	// 管理员列表
	public function adminlist() {
		$data = $this->getadminlist ();
		// var_dump($data);
		$this->assign ( array (
				'data' => $data
		) );
		$this->display ( 'admin/adminlist' );
	}
	
	// 删除管理员
	public function admindel() {
		if ($_GET ['id']) {
			echo $_GET ['id'];
			$this->deladmin ();
			$this->showmessage ( '删除管理员成功！', 'index.php?m=home&c=index&a=adminlist' );
		}
	}
	private function deladmin() {
		$this->newobj = new \Home\Model\adminModel ();
		return $this->newobj->del_by_id ( $_GET ['id'] );
	}
	// 角色列表
	public function rolelist() {
		$data = $this->getrolelist ();
		// var_dump($data);
		$this->assign ( array (
				'data' => $data 
		) );
		$this->display ( 'admin/rolelist' );
	}
	// 添加栏目
	public function columnadd() {
		if (! isset ( $_POST ['submit'] )) {
			$this->display ( 'admin/columnadd' );
		} else {
			$column = $_POST ['column'];
			$url = $_POST ['url'];
			if (empty ( $column ) || empty ( $url )) {
				$this->showmessage ( '请把栏目标题、内容填写完整再提交！', 'index.php?m=home&c=index&a=columnadd' );
			} else {
				$data = array (
						'column' => $column,
						'url' => $url 
				);
				M ( 'column' )->add ( $data );
				$this->showmessage ( '添加栏目成功！', 'index.php?m=home&c=index&a=columnlist' );
			}
		}
	}
	// 栏目列表
	public function columnlist() {
		$data = $this->getcolumnlist ();
		// var_dump($data);
		$this->assign ( array (
				'data' => $data 
		) );
		$this->display ( 'admin/columnlist' );
	}
	
	// 删除栏目
	public function columndel() {
		if ($_GET ['id']) {
			$this->delcolumn ();
			$this->showmessage ( '删除栏目成功！', 'index.php?m=home&c=index&a=columnlist' );
		}
	}
	private function delcolumn() {
		$this->newobj = new \Home\Model\columnModel ();
		return $this->newobj->del_by_id ( $_GET ['id'] );
	}
	// 删除角色
	public function roledel() {
		if ($_GET ['id']) {
			$this->delrole ();
			$this->showmessage ( '删除角色成功！', 'index.php?m=home&c=index&a=rolelist' );
		}
	}
	private function delrole() {
		$this->newobj = new \Home\Model\roleModel ();
		return $this->newobj->del_by_id ( $_GET ['id'] );
	}
	// 删除新闻
	public function newsdel() {
		if ($_GET ['id']) {
			$this->delnews ();
			$this->showmessage ( '删除新闻成功！', 'index.php?m=home&c=index&a=newslist' );
		}
	}
	//删除新闻
	private function delnews() {
		$this->newobj = new \Home\Model\newsModel ();
		return $this->newobj->del_by_id ( $_GET ['id'] );
	}
	private function getnewsinfo() {
		if (isset ( $_GET ['id'] )) {
			$id = intval ( $_GET ['id'] );
			$this->newobj = new \Home\Model\newsModel ();
			return $this->newobj->findOne_by_id ( $id );
		} else {
			return array ();
		}
	}
	// 修改角色
	public function role_update() {
		if ($_POST ['role']) {
			$where = array (
					'id' => $_POST ['id']
			);
			//echo "<script>alert($where['id'])</script>";
			$data ['role'] = $_POST ['role'];
			$data ['ManageRole'] = $_POST ['ManageRole'];
			$data ['ManageNews'] = $_POST ['ManageNews'];
			$data ['ManageUser'] = $_POST ['ManageUser'];
			$data ['ManageAdmin'] = $_POST ['ManageAdmin'];
			$data ['ManagePI'] = $_POST ['ManagePI'];
			if (M ( 'role' )->where ( $where )->save ( $data )) {
				$this->showmessage ( '角色修改成功', 'index.php?m=home&c=index&a=rolelist' );
			}
			else{
				//var_dump(M ( 'role' )->getLastSql());
				$this->showmessage ( '角色修改失败', 'index.php?m=home&c=index&a=role_update' );
			}
		} else {
			$where ['id'] = $_GET ['id'];
			echo '<br/>';
			$data = M ( 'role' )->where ( $where )->find ();
			$this->assign ( 'data', $data );
			$this->display ( "admin/role_update" );
		}
	}
	
	// 修改新闻
	public function news_update() {
		if ($_POST ['title']) {
			$where ['id'] = $_POST ['id'];
			$data ['title'] = $_POST ['title'];
			$data ['content'] = $_POST ['content'];
			$data ['author'] = $_POST ['author'];
			$data ['from'] = $_POST ['from'];
			$data ['column'] = $_POST ['column'];
			$data ['dateline'] = time ();
			$this->showmessage ( '修改成功', 'index.php?m=Home&c=Index&a=newslist' );
			var_dump ( $data );
			return M ( 'news' )->where ( $where )->save ( $data );
		} else {
			$where ['id'] = $_GET ['id'];
			$data1 = $this->getcolumnlist ();
			$this->assign ( array (
					'data1' => $data1
			) );
			$data = M ( 'news' )->where ( $where )->find ();
			$this->assign ( 'data', $data );
			$this->display ( "admin/news_update" );
		}
	}
	// 修改栏目
	public function column_update() {
		if ($_POST ['column']) {
			$where ['id'] = $_POST ['id'];
			$data ['column'] = $_POST ['column'];
			$data ['url'] = $_POST ['url'];
			M ( 'column' )->where ( $where )->save ( $data );
			$this->showmessage ( '修改成功', 'index.php?m=Home&c=Index&a=columnlist' );
			
		} else {
			$where ['id'] = $_GET ['id'];
			$data = M ( 'column' )->where ( $where )->find ();
			$this->assign ( array (
					'data' => $data
			) );
			var_dump($data);
			$this->display ( "admin/column_update" );
		}
	}
	
	// 新闻列表页
	public function newslist() {
		$data = $this->getnewslist ();
		// var_dump($data);
		$this->assign ( array (
				'data' => $data 
		) );
		
		$this->display ( 'admin/newslist' );
	}
	
	// 获取新闻列表
	private function getnewslist() {
		// $newsobj = M('news');
		$this->newsobj = new \Home\Model\newsModel ();
		return $this->newsobj->findAll_orderby_dateline ();
	}
	// 获取管理员列表
	private function getadminlist() {
		$this->newsobj = new \Home\Model\adminModel ();
		return $this->newsobj->findAll_orderby_dateline ();
	}
	// 获取会员列表
	private function getuserlist() {
		$this->newsobj = new \Home\Model\usersModel ();
		return $this->newsobj->findAll_orderby_dateline ();
	}
	// 获取角色列表
	private function getrolelist() {
		$this->newsobj = new \Home\Model\roleModel ();
		return $this->newsobj->findAll_orderby_dateline ();
	}
	// 获取栏目列表
	private function getcolumnlist() {
		$this->newsobj = new \Home\Model\columnModel ();
		return $this->newsobj->findAll_orderby_dateline ();
	}
	// 提示跳转函数
	private function showmessage($info, $url) {
		echo "<script>alert('$info');window.location.href='$url'</script>";
		exit ();
	}
}